package com.zeedev.flyingsaucer.beers.activities.beerdialog;

import android.app.Activity;
import android.content.ContentQueryMap;
import android.content.ContentValues;
import android.database.Cursor;
import android.text.TextUtils;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.zeedev.flyingsaucer.ufoclub.R;
import mobi.zeedev.flyingsaucer.common.DataModule.FlyingSaucerConv.Beers;
import mobi.zeedev.flyingsaucer.common.DataModule.MetaBeer;
import mobi.zeedev.flyingsaucer.common.DataModule.Util;

import java.util.Collection;
import java.util.Observable;
import java.util.Observer;

@Deprecated
public class BeerDialogModel implements Observer {
/*
    private final class HandlerExtension extends Handler {

        @Override
        public void handleMessage(Message msg) {
            Ln.d("Updated from obvserver property.");
            updateGui();
        }
    }*/


//    @InjectView(R.id.beer_dialog_4_parent)
//    View beerDialogParent;

    @Inject
    BeerDialog2 beerDialog2;

    private Cursor mCursor;
    private ContentQueryMap ccc;

    public static final Integer LAYOUT = R.layout.beer_dialog_4;

    private Activity mActivity;

    @Inject
    public BeerDialogModel(Activity mActivity, @Named(MetaBeer.BEER_ID) Integer beerId) {
        this.mActivity = mActivity;
        mCursor = mActivity.managedQuery(Beers.CONTENT_URI, null, Beers.BEER_ID + "=?", Util.asAr(beerId), null);
        ccc = new ContentQueryMap(mCursor, Beers.BEER_ID, true, null);
        ccc.addObserver(this);
    }


    public ContentValues getTopContentValues() {
        ContentValues ret = null;
        Collection<ContentValues> cvs = ccc.getRows().values();
        ContentValues[] cvar = cvs.toArray(new ContentValues[]{});
        if (cvar.length >= 1) {
            if (cvar[0] != null) {
                ContentValues cur = cvar[0];
                ret = new ContentValues(cur);

            }
        }
        return ret;
    }

    public Boolean hasRecord() {
        return !ccc.getRows().isEmpty();
    }

    public Boolean isRecordComplete() {
        Boolean ret = false;
        Collection<ContentValues> cvs = ccc.getRows().values();
        ContentValues[] cvar = cvs.toArray(new ContentValues[]{});
        if (cvar.length >= 1) {
            if (cvar[0] != null) {
                ContentValues cur = cvar[0];
                String desc = cur.getAsString(Beers.DESCRIPTION);
                ret = !TextUtils.isEmpty(desc);
            }
        }
        return ret;
    }


/*    public <A> A getBeerId(Class<A> type) {
    A ret = null;

    try {
        Method m = type.getDeclaredMethod("valueOf", String.class);
        ret = (A) m.invoke(null, this.beerId);
    } catch (Exception e) {
        Ln.e("Type conversion should be completeable.", e);
    }

    return ret;
}*/

    @Override
    public void update(Observable o, Object arg) {
//        beerDialog2.bindView();
        //TODO FIX

    }

}
